import gzip
import urllib.request
from http import cookiejar
from io import BytesIO

'''
1.手动复制pc上面的cookies 放在请求头里面
'''

# 1.url
url = "https://www.yaozh.com/member/"

# 2.添加请求头
header = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh,en;q=0.9,zh-CN;q=0.8",
    "Cache-Control": "max-age=0",
    "Connection": "keep-alive",
    "Cookie": "acw_tc=2f624a6015776950300263049e6c7df7bd3504fd430b7c94eeec19706e1ae9; PHPSESSID=1ugku4hthggnkhjdbh8n4vf7c4; _ga=GA1.2.1180355915.1577695031; _gid=GA1.2.364200878.1577695031; _gat=1; Hm_lpvt_65968db3ac154c3089d7f9a4cbb98c94=1577698106; yaozh_logintime=1577698111; yaozh_user=864282%09lucky_yu; yaozh_userId=864282; yaozh_jobstatus=kptta67UcJieW6zKnFSe2JyXnoaZbZlnnZaHnKZxanJT1qeSoMZYoNdzb5tantjIzdrD3dZZoKifnZ%2BDn5iorJDVop6Yg3HYnmpnm1pjmJy223C433f632E929AB4bd381989222290Zl52ckW%2BYV6DXn5VtWamhnsZbbKabZ5ieW2iWb2mZnpqSlZWTaJdXoOE%3D3408536aecac3d0f533637dd1650cfad; db_w_auth=741536%09lucky_yu; UtzD_f52b_saltkey=hEA8467v; UtzD_f52b_lastvisit=1577694512; UtzD_f52b_lastact=1577698112%09uc.php%09; UtzD_f52b_auth=bdafLtrpIJYeU0YWnhphHwpOE%2BgTngV9svfM7Y5hJ1iyScZV5Ojm7oh5zOqj4Wc7ia7vNE248KsCWymwVD19YR9A%2BY0; yaozh_uidhas=1; yaozh_mylogin=1577698121; acw_tc=2f624a6015776950300263049e6c7df7bd3504fd430b7c94eeec19706e1ae9; Hm_lvt_65968db3ac154c3089d7f9a4cbb98c94=1577695790%2C1577695833%2C1577697631%2C1577697988",
    "Host": "www.yaozh.com",
    "Referer": "https://www.yaozh.com/member/",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-origin",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
}

# 3.构建请求对象
requests = urllib.request.Request(url, headers=header)

# 4.发送请求对象
response = urllib.request.urlopen(requests)


# 5.读取数据
data = response.read()

#以下代码是解决报错：UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: inva
buff = BytesIO(data)
f = gzip.GzipFile(fileobj=buff)
res = f.read().decode('utf-8')


#6.写入文件
with open('01cookies.html', mode='w',encoding='utf-8') as f:
    f.write(res)
